package com.yuju.controller.hotel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.hotel.enums.RoomTypeReturnCodeEnum;
import com.yuju.hotel.model.RoomTypeModel;
import com.yuju.hotel.service.IRoomTypeService;
import com.yuju.util.Utils;
import com.yuju.vo.hotel.RoomTypeVO;
/**
 * 
 * 类名：RoomTypeController  <br />
 *
 * 功能：房间类型相关接口
 *
 * @author : zhousc <br />
 * @Date : 2017年11月27日 下午3:03:26  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("roomType")
public class RoomTypeController extends BaseController<RoomTypeVO, RoomTypeModel> {

	@Autowired
	private IRoomTypeService roomTypeService;
	
	/**
	 * 
	 * 功能：<br/>房型列表
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 下午3:06:32 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		RoomTypeVO  roomTypeVO = obj.toJavaObject(RoomTypeVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomTypeVO, "pager", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		PageModel pageModel = roomTypeService.pageList(convert(roomTypeVO), roomTypeVO.getPage(), roomTypeVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomTypeReturnCodeEnum.ROOMTYPE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomTypeReturnCodeEnum.ROOMTYPE_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：<br/>新增房型
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 下午3:07:03 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		RoomTypeVO  roomTypeVO = obj.toJavaObject(RoomTypeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomTypeVO, "save", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		int rows = roomTypeService.save(convert(roomTypeVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomTypeReturnCodeEnum.ROOMTYPE_SAVE_SUCCESS.getCode());
			result.setReturnMsg(RoomTypeReturnCodeEnum.ROOMTYPE_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomTypeReturnCodeEnum.ROOMTYPE_SAVE_FAILURE.getCode());
			result.setReturnMsg(RoomTypeReturnCodeEnum.ROOMTYPE_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：<br/>更新房型
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 下午3:07:16 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		RoomTypeVO  roomTypeVO = obj.toJavaObject(RoomTypeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomTypeVO, "update", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		int rows = roomTypeService.updateSelective(convert(roomTypeVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomTypeReturnCodeEnum.ROOMTYPE_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoomTypeReturnCodeEnum.ROOMTYPE_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomTypeReturnCodeEnum.ROOMTYPE_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoomTypeReturnCodeEnum.ROOMTYPE_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：<br/>房型删除
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 下午3:07:35 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		RoomTypeVO  roomTypeVO = obj.toJavaObject(RoomTypeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomTypeVO, "del", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		int rows = roomTypeService.del(roomTypeVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomTypeReturnCodeEnum.ROOMTYPE_DEL_SUCCESS.getCode());
			result.setReturnMsg(RoomTypeReturnCodeEnum.ROOMTYPE_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomTypeReturnCodeEnum.ROOMTYPE_DEL_FAILURE.getCode());
			result.setReturnMsg(RoomTypeReturnCodeEnum.ROOMTYPE_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>根据房型ID获取房型信息
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 下午3:07:49 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		RoomTypeVO  roomTypeVO = obj.toJavaObject(RoomTypeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomTypeVO, "get", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		RoomTypeModel model = roomTypeService.findById(roomTypeVO.getId());
		if (Utils.isNotNull(model)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(model);
			result.setReturnCode(RoomTypeReturnCodeEnum.ROOMTYPE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomTypeReturnCodeEnum.ROOMTYPE_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：房型下拉列表 弹窗通用<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月7日 下午2:28:10 <br/>
	 */
	@RequestMapping("roomTypeDropdownList")
	@ResponseBody
	public Result roomTypeDropdownList(@RequestBody JSONObject obj) {
		RoomTypeVO  roomTypeVO = obj.toJavaObject(RoomTypeVO.class);
		List<RoomTypeModel> list = roomTypeService.list(convert(roomTypeVO));
		Result result = new Result();
		result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
		result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
		result.setData(list);
		result.setReturnCode(RoomTypeReturnCodeEnum.ROOMTYPE_QUERY_SUCCESS.getCode());
		result.setReturnMsg(RoomTypeReturnCodeEnum.ROOMTYPE_QUERY_SUCCESS.getDesc());
		return result;
	}
	/**
	 * 
	 * 功能：入住标准编辑页 房型列表<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月7日 下午2:28:10 <br/>
	 */
	@RequestMapping("roomTypeCheckinStandardList")
	@ResponseBody
	public Result roomTypeCheckinStandardList(@RequestBody JSONObject obj) {
		RoomTypeVO  roomTypeVO = obj.toJavaObject(RoomTypeVO.class);
		List<RoomTypeModel> list = roomTypeService.list(convert(roomTypeVO));
		//入住标准房型列表展示字段需重置
		
		List<RoomTypeModel> returnList = new ArrayList<RoomTypeModel>();
		for(int i = 0; i < list.size(); i++){
			RoomTypeModel roomTypeModel = new RoomTypeModel();
			roomTypeModel.setRoomTypeId(list.get(i).getId());
			roomTypeModel.setRoomTypeName(list.get(i).getName());
			roomTypeModel.setRoomTypePrice(list.get(i).getPrice());
			roomTypeModel.setRoomTypeMonthlyPrice(list.get(i).getMonthPrice());
			returnList.add(roomTypeModel);
		}
		Result result = new Result();
		result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
		result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
		result.setData(returnList);
		result.setReturnCode(RoomTypeReturnCodeEnum.ROOMTYPE_QUERY_SUCCESS.getCode());
		result.setReturnMsg(RoomTypeReturnCodeEnum.ROOMTYPE_QUERY_SUCCESS.getDesc());
		return result;
	}
	/**
	 * 
	 * 功能：重名验证<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月14日 上午10:33:53 <br/>
	 */
	@RequestMapping("selectbyname")
	@ResponseBody
	public Result selectbyname(@RequestBody JSONObject obj) {
		RoomTypeVO  roomTypeVO = obj.toJavaObject(RoomTypeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomTypeVO, "selectbyname", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		boolean selectByName = roomTypeService.selectByName(convert(roomTypeVO));
		if (selectByName) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}
}
